<template>
    <el-dialog :visible.sync="visible" title="采购入库">
      <el-form label-width="100px">
        <el-form-item label="商品ID">
          <el-input v-model="data.goods_id"  @blur="findGoods"/>
        </el-form-item>
        <el-form-item label="商品名">
          <el-input v-model="data.goods_name" disabled/>
        </el-form-item>
        <el-form-item label="采购价格">
          <el-input v-model="data.price"/>
        </el-form-item>
        <el-form-item label="数量">
          <el-input-number v-model="data.num" :min="1"/>
        </el-form-item>
        <el-form-item label="供应商">
          <el-input v-model="data.provider"/>
        </el-form-item>
        <div style="text-align: center; margin-top: 10px;">
            <el-button type="primary" icon="el-icon-edit" @click="save">保存</el-button>
        </div>
      </el-form>
    </el-dialog>
</template>

<script>
    export default {
        name: "stockInDialog",
      data(){
          return {
            visible: false,
            data:{
              ...this.initData()
            }
          }
      },
      methods:{
          save(){
            if(!this.data.goods_name){
              this.$message.error("请填写正确的商品ID")
              return
            }
            this.$http({
              url: this.$http.adornUrl('/sys/storage/saveStockDetail'),
              method: 'post',
              data: this.$http.adornData(
                this.data
              )
            }).then(({ data }) => {
              this.$message("入库成功")
              this.$emit("ok")
              this.visible = false
            })

          },
          findGoods(){
            var id = this.data.goods_id
            this.$http({
              url: this.$http.adornUrl('/sys/storage/findGoods'),
              method: 'get',
              params: this.$http.adornParams(
                {
                  id,//: v.id
                }
              )
            })
            .then(({data}) => {
              if(data.length){
                this.data.goods_name = data[0].prod_name
              }
            })
          },
          initData(){
            return {
              stock_id:"",
              goods_id: "",
              goods_name:"",
              price:"",
              num:"",
              provider: ""
            }
          },
          reset(){
            this.data = this.initData()
          },
          show(stockId){
            this.reset()
            this.data.stock_id = stockId
            this.visible = true
          }
      }
    }
</script>

<style scoped>

</style>
